<?php

declare (strict_types=1);

namespace app\shop\controller;

use app\shop\model\shop\Category as ShopCategoryModel;
use cores\exception\BaseException;
use think\response\Json;
use app\shop\model\shop\Shop as ShopModel;
use app\shop\service\store\User as ShopUserService;
use app\shop\model\shop\Detail as DetailModel;
/**
 * 商户管理
 * Class Store
 * @package app\admin\controller
 */
class Shop extends Controller
{
    /**
     * 强制验证当前访问的控制器方法method
     * @var array
     */
    protected array $methodRules = [
        'index' => 'GET',
        'recycle' => 'GET',
        'add' => 'POST',
        'move' => 'POST',
        'delete' => 'POST',
    ];

    /**
     * 商城列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function index(): Json
    {
        // 商城列表
        $model = new ShopModel;
        $list = $model->getList();
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 获取商城登录token
     * @param int $storeId
     * @return Json
     */
    public function superLogin(int $storeId): Json
    {
        // 获取指定商城的管理员用户信息
        $userInfo = ShopUserService::getUserInfoByShopId($storeId);
        if (empty($userInfo)) {
            return $this->renderError('未找到该商城管理员用户');
        }
        // 登录商户后台
        $token = ShopUserService::login($userInfo->toArray());
        return $this->renderSuccess([
            'userId' => $userInfo['store_user_id'],
            'token' => $token
        ]);
    }

    /**
     * 回收站列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function recycle(): Json
    {
        // 商城列表
        $model = new ShopModel;
        $list = $model->getList(true);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 新增商城
     * @return Json
     */
    public function add(): Json
    {
        // 新增记录
        $model = new ShopModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 移入回收站
     * @param int $storeId
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function recovery(int $storeId): Json
    {
        // 商城详情
        $model = ShopModel::detail($storeId);
        if (!$model->recycle()) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 移出回收站
     * @param int $storeId
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function move(int $storeId): Json
    {
        // 商城详情
        $model = ShopModel::detail($storeId);
        if (!$model->recycle(false)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除商城
     * @param int $storeId
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(int $storeId): Json
    {
        // 商城详情
        $model = ShopModel::detail($storeId);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }


    public function getDetail(): Json
    {
        //商城详情
        $detail = DetailModel::detail($this->shopId);
        return $this->renderSuccess(compact('detail'));
    }

    public function getAddress(): Json
    {
        //商城详情
        $detail = DetailModel::address($this->shopId);
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 店铺基础信息编辑
     * @param int $categoryId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editDetail(): Json
    {
        $detailModel = new DetailModel();
        // 更新记录
        if ($detailModel->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($detailModel->getError() ?: '更新失败');
    }

    /**
     * 店铺地址信息编辑
     * @param int $categoryId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editAddress(): Json
    {
        $detailModel = new DetailModel();
        // 更新记录
        if ($detailModel->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($detailModel->getError() ?: '更新失败');
    }
}
